package database;

import java.sql.Connection;
import java.sql.PreparedStatement;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Insert extends JInternalFrame {
	
	Connection conn;
	DatabaseTest dt;
	
	private JTextField codeIn;
	private JTextField eaIn;
	private JLabel lblNewLabel;
	private JLabel lblNewLabel_1;
	private JButton btnInsert;
	private JLabel status;

	public Insert(Connection c, DatabaseTest d) {
		super("입력",true,true,true,false);
		setBounds(0, 0, 200, 200);
		getContentPane().setLayout(null);
		getContentPane().add(getCodeIn());
		getContentPane().add(getEaIn());
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getLblNewLabel_1());
		getContentPane().add(getBtnInsert());
		getContentPane().add(getStatus());
		this.conn=c;
		this.dt=d;
	}
	private JTextField getCodeIn() {
		if (codeIn == null) {
			codeIn = new JTextField();
			codeIn.setBounds(70, 10, 102, 21);
			codeIn.setColumns(10);
		}
		return codeIn;
	}
	private JTextField getEaIn() {
		if (eaIn == null) {
			eaIn = new JTextField();
			eaIn.setColumns(10);
			eaIn.setBounds(70, 41, 102, 21);
		}
		return eaIn;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("Code");
			lblNewLabel.setBounds(12, 13, 46, 15);
		}
		return lblNewLabel;
	}
	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("Ea");
			lblNewLabel_1.setBounds(12, 44, 46, 15);
		}
		return lblNewLabel_1;
	}
	private JButton getBtnInsert() {
		if (btnInsert == null) {
			btnInsert = new JButton("저장");
			btnInsert.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try{
						String cin = codeIn.getText();
						int ein = Integer.parseInt(eaIn.getText());
						
						String sql = "insert into ptbl(serial, code, ea) values(ptbl_serial.nextval,?,?)";
						
						PreparedStatement  pstmt = conn.prepareStatement(sql);
						pstmt.setString(1, cin);
						pstmt.setInt(2, ein);
						
						pstmt.executeUpdate();
						
						status.setText(codeIn.getText() + "가 저장 되었습니다.");
						codeIn.setText("");
						eaIn.setText("");
						
						pstmt.close();
						if(dt.select!=null)	dt.select.searchAll();
						
					}catch(Exception ex){
						ex.printStackTrace();
					}
					
					
					
					
				}
			});
			btnInsert.setBounds(70, 72, 102, 23);
		}
		return btnInsert;
	}
	private JLabel getStatus() {
		if (status == null) {
			status = new JLabel("코드와 수량을 입력하세요.");
			status.setBounds(12, 146, 160, 15);
		}
		return status;
	}
}
